package com.gps.skyrc.data;

import com.gps.skyrc.bean.DeviceInfo;
import com.gps.skyrc.bean.HistoryData;
import com.gps.skyrc.bean.testBean;
import com.gps.skyrc.tool.StaticUtils;

import java.sql.Time;
import java.util.ArrayList;
import java.util.TimeZone;

/**
 * Create by ywq
 * data 2018/12/5
 */
public class ParseData {
    //全是已经转成10进制的了

    public static DeviceInfo parsehdop(DeviceInfo info, byte[] data) {
        info.setSatelliteNum(data[0] & 0xff);
        info.setHdop(((double) (data[1] & 0xff)) / 10);
        info.setVoltagePercent(data[2] & 0xff);
        info.setIsHaveNewMsg(data[3] & 0xff);
        info.setIsTesting(data[4] & 0xff);
        info.setTestMode(data[5] & 0xff);
        info.setGpsBtnStatue(data[6] & 0xff);
        return info;
    }


    public static Float parseVersion(byte[] data) {
        int a = data[11] & 0xff;
        int b = data[12] & 0xff;
        Float version = (a * 100 + b) / 100f;
        return version;
    }

    public static HistoryData parsehistoryList(byte[] data, String mac) {
        int size = data[4] * 256 + data[5];
        HistoryData bean = new HistoryData();
        bean.setMac(mac);
        bean.setMode(data[6]);
        //2018/09/11/15/30/26 加时区
        int rawOffset = TimeZone.getDefault().getRawOffset() / 3600000;
        String timestamp = String.valueOf(data[7] + 2000) + "/"
                + StaticUtils.stringFormat("%02d", data[8]) + "/"
                + StaticUtils.stringFormat("%02d", data[9]) + "/"
                + StaticUtils.stringFormat("%02d", data[10] + rawOffset) + "/"
                + StaticUtils.stringFormat("%02d", data[11]) + "/"
                + StaticUtils.stringFormat("%02d", data[12]);
        String tamp = StaticUtils.timesTamp(timestamp, "yyyy/MM/dd/HH/mm/ss");
        bean.setTimestamp(Long.parseLong(tamp));
        bean.setHistoryDetailId(data[13]);
        bean.setValid(data[14] == 0);
        return bean;
    }


    public static HistoryData parseHistoryDetail(byte[] data, HistoryData historyData) {
        int mode = data[4];
        historyData.setMode(mode);
        if (mode == 0 || mode == 1) {
            if (mode == 0) {//速度
                historyData.setStartSpeed(data[5] & 0xff);
                historyData.setEndSpeed((data[6] & 0xff) * 256 + (data[7] & 0xff));
            } else {//距离
                historyData.setStartDistance(data[5] & 0xff);
                historyData.setEndDistance((data[6] & 0xff) * 256 + (data[7] & 0xff));
            }
            historyData.setTime((data[8] * 60 + data[9]) * 1000 + data[10] * 10);
            float mslBase = (data[11] * 256 + data[12]) / 10;

            ArrayList<Float> speedList = new ArrayList<>();
            ArrayList<Float> accelerationList = new ArrayList<>();
            int cycleTime = data.length - 14;//循环次数
            for (int i = 0; i < cycleTime; i++) {
                float speedSingle = (float) ((data[11 + i] & 0xff) * 1.852);//海里单位 要转
                float a = 0;//加速度值
                if (i == 0) {
                    if (mode == 0) {
                        a = (float) ((speedSingle - (data[5] & 0xff) * 1.852) * 10 / 36 / 0.2 / 9.8);
                    } else {
                        a = (float) ((speedSingle - 0) * 1.852 * 10 / 36 / 0.2 / 9.8);
                    }
                } else {
                    a = (float) ((speedSingle - (data[11 + (i - 1)] & 0xff) * 1.852) * 10 / 36 / 0.2 / 9.8);
                }
                speedList.add(speedSingle);
                accelerationList.add(a);
            }
            historyData.setSpeedList(speedList);
            historyData.setAccelerationList(accelerationList);
        } else if (mode == 2 || mode == 3) {//绝对值
            historyData.setAverageSpeed((double) (((data[5] & 0xff) * 256) + (data[6] & 0xff)) / 100);
            historyData.setDistance((data[7] & 0xff) * 256 + data[8] & 0xff);
            historyData.setTime((data[9] & 0xff) * 60 + data[10] & 0xff);
            historyData.setMaxSpeed((double) (((data[11] & 0xff) * 256) + (data[12] & 0xff)) / 100);
            if (mode == 2)
                historyData.setSlope((float) (((data[13] & 0xff) * 256) + (data[14] & 0xff)) / 100);
            else
                historyData.setMaxHeight(((data[13] & 0xff) * 256) + (data[14] & 0xff));

        }
        return historyData;
    }

    public static testBean parseTest(byte[] data) {
        testBean testBean = new testBean();
        String time = String.valueOf(data[0] & 0xff) + "年" + String.valueOf(data[1] & 0xff) + "月" + String.valueOf(data[2] & 0xff) + "日" + String.valueOf(data[3] & 0xff)
                + "时" + String.valueOf(data[4] & 0xff) + "分" + String.valueOf(data[5] & 0xff) + "秒" + "(未加时区)";
        double speed = ((double) (data[6] & 0xff) * 256 + (data[7] & 0xff)) * 1.852 / 100;
        double maxSpeed = ((double) (data[10] & 0xff) * 256 + (data[11] & 0xff)) * 1.852 / 100;
        int slope = (data[8] & 0xff) * 256 + (data[9] & 0xff);
        testBean.setTime(time);
        testBean.setSpeed(speed);
        testBean.setSlope(slope);
        testBean.setMaxSpeed(maxSpeed);
        return testBean;
    }

    public static int parseCleanCache(byte[] data) {
        return data[4] & 0xff;
    }
}
